package com.inifitness.common.rabbitmq.producer;

import com.inifitness.common.rabbitmq.CustomRabbitConfig;
import com.inifitness.common.rabbitmq.message.DelayedMessage;

import cn.hutool.core.lang.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * TODO: (sunjinfeng 2024-05-14)
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024-05-14 12:58
 */
@Component
@Log4j2
@RequiredArgsConstructor
public class MailProducer {

  private final RabbitTemplate rabbitTemplate;
  private static final long DELAY = 3600L;

  public void sendMail(String msg) {
    log.info("send mail: {}", msg);
    DelayedMessage message = new DelayedMessage(UUID.fastUUID().toString(), msg);
    rabbitTemplate.convertAndSend(CustomRabbitConfig.DELAYED_EXCHANGE,
        CustomRabbitConfig.DELAYED_SMS_ROUTING_KEY, message, t -> {
          t.getMessageProperties().setDelayLong(DELAY);
          return t;
        });
  }
}
